﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.Threading;

namespace JustPlus
{
    /// <summary>
    /// OperationPanel.xaml 的交互逻辑
    /// </summary>
    public partial class OperationPanel : UserControl
    {
        //public static string logoutUrl="";
        private float sopacity = 0.6f;
        private Image[] ImageCell;
        private string[] stringCell;
        public OperationPanel()
        {
            InitializeComponent();
            ImageCell = new Image[8] { image1, image2, image3, image4, image5, image6, image7, image8 };
            stringCell = new string[8] { "频道选择", "搜索音乐", "音乐基因", "登录", "设置", "帮助", "更新", "关于" };
            SetOpacity(0.0f); 
        }

        public void SetOpacity(float opacity)
        {
            foreach (Image image in ImageCell)
                image.Opacity = opacity;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 8; i++)
            {
                ImageCell[i].MouseEnter += new MouseEventHandler(Elem_MouseEnter);
                ImageCell[i].MouseLeave += new MouseEventHandler(Elem_MouseLeave);
                ImageCell[i].Tag = stringCell[i];
            }
        }

        private void Elem_MouseEnter(object sender, MouseEventArgs e)
        {
            Point pt = e.GetPosition(this);
            //System.Diagnostics.Debug.WriteLine(pt.X.ToString() + " " + pt.Y.ToString());
            if (e.LeftButton == MouseButtonState.Released && pt.X + pt.Y <= 1)
                return;
            Image image = sender as Image;
            image.Opacity = 1.0;
            label1.Content = image.Tag.ToString();
        }

        private void Elem_MouseLeave(object sender, MouseEventArgs e)
        {
            Point pt = e.GetPosition(this);
            Image image = sender as Image;
            image.Opacity = sopacity;
            label1.Content = "";
        }

        private void Grid_MouseEnter(object sender, MouseEventArgs e)
        {
            Point pt = e.GetPosition(this);
            //System.Diagnostics.Debug.WriteLine("Leave:" + pt.X.ToString() + " " + pt.Y.ToString() + e.LeftButton.ToString());
            if (e.LeftButton == MouseButtonState.Released && pt.X + pt.Y <= 1)
                return;
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            mwnd.stackPanel2.Background = new SolidColorBrush(Colors.Transparent);

            SetOpacity(sopacity);
            Grid grid = sender as Grid;
            grid.ShowGridLines = true;
        }

        private void Grid_MouseLeave(object sender, MouseEventArgs e)
        {
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            //MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            //修订：窗口退出时，触发leave事件，但是mwnd=null
            if (mwnd == null )
                return;
            SetOpacity(0.0f);
            Grid grid = sender as Grid;
            grid.ShowGridLines = false;
            mwnd.stackPanel2.Background = PlayerPanel.bkbrush;   
        }

        //==>登录豆瓣账户
        private void image4_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //PlayerPanel.HasLogined();
            ThreadPool.QueueUserWorkItem(new WaitCallback((state) =>
            {
                string loginresult = new ConnectionBase().Get(@"http://douban.fm/");
                if (string.IsNullOrEmpty(loginresult))
                    LoginPanel.HasLoginOn = false;
                else
                {
                    MatchCollection mc = Regex.Matches(loginresult, @"<div class=""login"">(\s.*?)*</ul>", RegexOptions.IgnoreCase);
                    if (mc.Count == 0)
                    {
                        LoginPanel.HasLoginOn = false;
                    }
                    else
                    {
                        string temp = mc[0].Groups[0].Value;
                        string userurl = Regex.Match(temp, @"<a href=\""([^\""]+)\"" id=\""fm-user\"">", RegexOptions.IgnoreCase).Groups[1].Value;
                        string username = Regex.Match(temp, @"id=\""fm-user\"">([^\""]+)<i></i></a>", RegexOptions.IgnoreCase).Groups[1].Value;
                        if (userurl == @"{{= user.url }}" || username == @"{{= user.name }}")
                            LoginPanel.HasLoginOn = false;
                        else
                        {
                            LoginPanel.HasLoginOn = true;
                            string played = Regex.Match(temp, @"<a href=""/mine\?type=played"" target=""_blank"">累积收听<i>(\d+)</i>首</a>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
                            string liked = Regex.Match(temp, @"<a href=""/mine\?type=liked"" target=""_blank"">加红心<i>(\d+)</i>首</a>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
                            string banned = Regex.Match(temp, @"<a href=""/mine\?type=banned"" target=""_blank""><i>(\d+)</i>首不再播放</a>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
                            LoginPanel.LoginName = username;
                            LoginPanel.LoginPlayed = Int32.Parse(played);
                            LoginPanel.LoginLiked = Int32.Parse(liked);
                            LoginPanel.LoginBanned = Int32.Parse(banned);
                        }
                    }
                    
                }
                Dispatcher.Invoke(new Action(() =>
                {
                    if (!LoginPanel.HasLoginOn)
                    {
                        MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
                        mwnd.stackPanel1.Children.Clear();
                        LoginPanel loginpanel = new LoginPanel();
                        mwnd.stackPanel1.Children.Add(loginpanel);
                    }
                    else
                    {
                        MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
                        mwnd.stackPanel1.Children.Clear();
                        LogoutPanel logoutpanel = new LogoutPanel();
                        mwnd.stackPanel1.Children.Add(logoutpanel);
                        logoutpanel.label1.Content = LoginPanel.LoginName;
                        logoutpanel.label2.Content = "累计收听" + LoginPanel.LoginPlayed.ToString() + "首";
                        logoutpanel.label3.Content = "加红心" + LoginPanel.LoginLiked.ToString() + "首";
                        logoutpanel.label4.Content = "不再收听" + LoginPanel.LoginBanned.ToString() + "首";
                    }
                }));
            }));
        }

        //==>关于软件
        public void image8_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            AboutPanel aboutpanel = new AboutPanel();
            mwnd.stackPanel1.Children.Clear();
            mwnd.stackPanel1.Children.Add(aboutpanel);
        }

        //==>软件设置
        public void image5_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            SettingPanel settingpanel = new SettingPanel();
            mwnd.stackPanel1.Children.Clear();
            mwnd.stackPanel1.Children.Add(settingpanel);
        }

        //==>频道选择
        private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            ChannelPanel channelpanel = new ChannelPanel();
            mwnd.stackPanel1.Children.Clear();
            mwnd.stackPanel1.Children.Add(channelpanel);
        }

        //==>帮助信息
        private void image6_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            HelpPanel helppanel = new HelpPanel();
            mwnd.stackPanel1.Children.Clear();
            mwnd.stackPanel1.Children.Add(helppanel);
        }

        //==>音乐基因
        private void image3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            GenePanel genepanel = new GenePanel();
            mwnd.stackPanel1.Children.Clear();
            mwnd.stackPanel1.Children.Add(genepanel);
        }

        //==>搜索音乐
        private void image2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            SearchPanel searchpanel = new SearchPanel();
            mwnd.stackPanel1.Children.Clear();
            mwnd.stackPanel1.Children.Add(searchpanel);
        }

        //==>更新
        public void image7_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MainWindow mwnd = (MainWindow)Application.Current.MainWindow;
            UpdatePanel updatepanel = new UpdatePanel();
            mwnd.stackPanel1.Children.Clear();
            mwnd.stackPanel1.Children.Add(updatepanel);
        }
    }
}
